package per.hx.bs.sys.serviceimpl;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import per.hx.bs.base.domain.PageDTO;
import per.hx.bs.constant.Setting;
import per.hx.bs.sys.dao.SysDictDao;
import per.hx.bs.sys.dao.SysNotifyDao;
import per.hx.bs.sys.dao.SysNotifyRecordDao;
import per.hx.bs.sys.dao.SysUserDao;
import per.hx.bs.entity.sys.SysDictDO;
import per.hx.bs.entity.sys.SysNotifyDO;
import per.hx.bs.entity.sys.SysNotifyRecordDO;
import per.hx.bs.entity.sys.SysUserDO;
import per.hx.bs.util.DateUtils;
import per.hx.bs.util.Query;
import per.hx.bs.vo.NotifyVO;

import java.util.*;

/**
 * @author HuXing
 * @date 2018/3/30  18:06
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping(value = "sys-service/notify")
public class SysNotifyApiServiceImpl {

    private final
    SysNotifyDao sysNotifyDao;
    private final
    SysNotifyRecordDao sysNotifyRecordDao;
    private final
    SysUserDao sysUserDao;
    private final
    SysDictDao sysDictDao;

    @Autowired
    public SysNotifyApiServiceImpl(SysNotifyDao sysNotifyDao, SysNotifyRecordDao sysNotifyRecordDao, SysUserDao sysUserDao, SysDictDao sysDictDao) {
        this.sysNotifyDao = sysNotifyDao;
        this.sysNotifyRecordDao = sysNotifyRecordDao;
        this.sysUserDao = sysUserDao;
        this.sysDictDao = sysDictDao;
    }


    /**
     * 通过id获取通知信息
     *
     * @param id 通知id
     * @return 通知信息
     */
    @GetMapping(value = "/get/{id}")
    public SysNotifyDO get(@PathVariable("id") Long id) {
        SysNotifyDO sysNotifyDO = sysNotifyDao.selectById(id);
        if (sysNotifyDO != null) {
            SysDictDO sysDictDO = sysDictDao.selectById(sysNotifyDO.getType());
            if (sysDictDO != null) {
                sysNotifyDO.setType(sysDictDO.getName());
            }
        }
        return sysNotifyDO;
    }

    /**
     * 按条件查询
     *
     * @param map 插叙条件
     * @return 通知集合
     */
    @PostMapping(value = "/list")
    public List<SysNotifyDO> list(@RequestBody Map<String, Object> map) {
        Query query = new Query(map);
        List<SysNotifyDO> sysNotifyDOS = sysNotifyDao.list(query);
        if (!sysNotifyDOS.isEmpty()) {
            for (SysNotifyDO sysNotifyDO : sysNotifyDOS) {
                if (sysNotifyDO != null) {
                    SysDictDO sysDictDO = sysDictDao.selectById(sysNotifyDO.getType());
                    if (sysDictDO != null) {
                        sysNotifyDO.setType(sysDictDO.getName());
                    }
                }
            }
        }
        return sysNotifyDOS;
    }

    /**
     * 按条件统计
     *
     * @param map 查询条件
     * @return 数量
     */
    @PostMapping(value = "/count")
    public int count(@RequestBody Map<String, Object> map) {
        Query query = new Query(map);
        return sysNotifyDao.count(query);
    }

    /**
     * 保存通知
     *
     * @param sysNotifyDO 通知
     * @return 影响行数
     */
    @PostMapping(value = "/save")
    public int save(@RequestBody SysNotifyDO sysNotifyDO) {
        int insert = sysNotifyDao.insert(sysNotifyDO);
        if (insert > 0) {
            Long[] userIds = sysNotifyDO.getUserIds();
            Long sysNotifyId = sysNotifyDO.getId();
            try {
                if (userIds.length > 0) {
                    for (Long userId : userIds) {
                        if (userId != null && userId != 0L) {
                            SysNotifyRecordDO recordDO = new SysNotifyRecordDO();
                            recordDO.setNotifyId(sysNotifyId);
                            recordDO.setUserId(userId);
                            recordDO.setIsRead(Setting.SYS_NOTIFY_READ_NO);
                            recordDO.setCreateDate(sysNotifyDO.getCreateDate());
                            recordDO.setCreateId(sysNotifyDO.getCreateId());
                            recordDO.setUpdateDate(sysNotifyDO.getCreateDate());
                            sysNotifyRecordDao.insert(recordDO);
                        }
                    }
                }
            } catch (RuntimeException e) {
                return 0;
            }
        }
        return insert;
    }

    /**
     * 更新通知
     *
     * @param sysNotifyDO 通知
     * @return 影响行数
     */
    @PutMapping(value = "/update")
    public int update(@RequestBody SysNotifyDO sysNotifyDO) {
        sysNotifyDO.setUpdateDate(new Date());
        return sysNotifyDao.updateById(sysNotifyDO);
    }

    /**
     * 按id删除
     *
     * @param id id
     * @return 影响行数
     */
    @DeleteMapping(value = "/remove")
    public int remove(@RequestParam("id") Long id) {
        return sysNotifyDao.deleteById(id);
    }

    /**
     * 批量删除
     *
     * @param ids IDS
     * @return 影响行数
     */
    @DeleteMapping(value = "/batchRemove")
    public int batchRemove(@RequestParam("ids") Long[] ids) {
        return sysNotifyDao.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 按条件查询
     *
     * @param map 查询条件
     * @return 查询结果
     */
    @PostMapping(value = "/selfList")
    public String selfList(@RequestBody Map<String, Object> map) {
        Query query = new Query(map);
        List<NotifyVO> notifyVOS = sysNotifyDao.listVO(query);
        if (!notifyVOS.isEmpty()) {
            for (NotifyVO notifyVO : notifyVOS) {
                notifyVO.setBefore(DateUtils.getTimeBefore(notifyVO.getCreateDate()));
                SysDictDO sysDictDO = sysDictDao.selectById(notifyVO.getType());
                if (sysDictDO != null) {
                    notifyVO.setType(sysDictDO.getName());
                }
                SysUserDO sysUserDO = sysUserDao.selectById(notifyVO.getCreateId());
                if (sysUserDO != null) {
                    notifyVO.setSender(sysUserDO.getName());
                }
            }
            return JSON.toJSONString(new PageDTO(sysNotifyDao.countVO(query), notifyVOS));
        } else {
            return JSON.toJSONString(new PageDTO(0, ""));
        }
    }

}
